<template>
  <div style="width: 500px;height: 900px;margin: 0 auto">
    <el-card>
      <el-form ref="form" :model="orders" label-width="80px">

        <el-form-item label="姓名">
          <el-input v-model="orders.username" style="width: 220px;float: left"></el-input>
        </el-form-item>

        <el-form-item label="租赁城市">
          <el-select v-model="orders.cityId" style="float: left;">
            <el-option v-for="city in cityArr"
                       :label="city.name" :value="city.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="取车时间">
          <div style="float: left">
            <el-date-picker
                v-model="orders.pickUpTime"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>

        <el-form-item label="还车时间">
          <div style="float: left">
            <el-date-picker
                v-model="orders.returnTime"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
          <el-button @click="Cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "PayView",
  data() {
    return {
      cityArr: [],
      orders: {
        username: "",
        cityId: "",
        pickUpTime: "",
        returnTime: "",
        carId: new URLSearchParams(location.search).get('id'),
      }
    }
  },
  methods: {
    Cancel(){
      let id = new URLSearchParams(location.search).get('id');
      this.$router.push("/detail?id="+id)
    },
    onSubmit() {
      let data = this.qs.stringify(this.orders);
      let type = new URLSearchParams(location.search).get('type')
      console.log(type)
      console.log(data)
      this.axios.post("http://localhost:19485/orders/"+type+"/create",this.orders, {
        headers: {
          'Content-Type': 'application/json'
        }})
          .then((response)=>{
            if (response.data.state==20000){
              this.$message.success("提交成功!");
              let id = new URLSearchParams(location.search).get('id');
              this.$router.push("/detail?id="+id)
            } else{
              this.$message.warning("提交失败!");
            }
          })
    }
  },
  mounted() {
    this.axios.get("http://localhost:19483/city")
        .then((response) => {
          if (response.data.state == 20000) {
            this.cityArr = response.data.data;
          }
        });
  }
}

</script>

<style scoped>

</style>